package com.lovo.test.zuoye;

import com.lovo.test.Util;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Doudizhu extends JFrame {
    private List<Card> list = new ArrayList();
    private List<Card> player1 = new ArrayList();
    private List<Card> player2 = new ArrayList();
    private List<Card> player3 = new ArrayList();
    private List<Card> base = new ArrayList();
    private String pokevaule = "";
    private String[] colors = {"♠", "♥", "♣", "♦"};
    private String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
    private Image bgimg = Util.getImage("/image/ddz.jpg");
    private Icon jButtonimage = Util.getIcon("/image/kaishi.png");
    private Image image = Util.getImage("/image/bd.png");

    public Doudizhu(){
        //设置背景图片
        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgimg,0,0,1400,1200,this);
            }
        });
        //设置空布局
        this.setLayout(null);
        this.init();
        this.xipai();
        this.fapai();
        int y = 720;
        for (int i = 0; i < 17; i++) {
            Icon icon = Util.getIcon("/image/" + player1.get(i).getImage() + ".jpg");
            JLabel jLabel = new JLabel(icon);
            jLabel.setBounds(100, y -= 35, 105, 150);
            this.add(jLabel);
        }
        int x = 950;
        for (int i = 0; i < 17; i++) {
            Icon icon = Util.getIcon("/image/" + player2.get(i).getImage() + ".jpg");
            JLabel jLabel = new JLabel(icon);
            jLabel.setBounds(x -= 35, 830, 105, 150);
            this.add(jLabel);
        }
        int y1 = 720;
        for (int i = 0; i < 17; i++) {
            Icon icon = Util.getIcon("/image/" + player3.get(i).getImage() + ".jpg");
            JLabel jLabel = new JLabel(icon);
            jLabel.setBounds(1170, y1 -= 35, 105, 150);
            this.add(jLabel);
        }
        int x1 = 720;
        for (int i = 0; i < 3; i++) {
            Icon icon = Util.getIcon("/image/" + base.get(i).getImage() + ".jpg");
            JLabel jLabel = new JLabel(icon);
            jLabel.setBounds(x1 -= 35, 100, 105, 150);
            this.add(jLabel);
        }
        JButton jButton = new JButton(jButtonimage);
        jButton.setBounds(634,480,134,35);
        this.add(jButton);
        //设置窗体大小
        this.setSize(1400, 1200);
        //设置标题图标
        this.setIconImage(image);
        //设置窗体可见
        this.setVisible(true);
        //窗体关闭程序结束
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //窗体居中显示
        this.setLocationRelativeTo(null);
        //设置窗体的标题文字
        this.setTitle("斗地主");
    }

    public static void main(String[] args) {
        new Doudizhu();


    }

    // 初始化54张扑克牌
    public void init() {
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < colors.length; j++) {
              pokevaule = (i + 3) + "_" + (j + 1);
                list.add(new Card(num[i],colors[j], i, j,pokevaule));
            }
        }
        list.add(new Card("", "小王",14,0,"16_1"));
        list.add(new Card("", "大王",15,0,"17_1"));
    }

    // 洗牌
    public void xipai() {
        Collections.shuffle(list);
    }

    // 发牌
    public void fapai() {
        //sublist
        player1 = list.subList(0, 17);
        player2 = list.subList(17, 34);
        player3 = list.subList(34, 51);
        base = list.subList(51, 54);
        sort(player1);
        sort(player2);
        sort(player3);
        System.out.println("玩家一：" + player1);
        System.out.println("玩家二：" + player2);
        System.out.println("玩家三：" + player3);
        System.out.println("底牌：" + base);
    }

    // 排序
    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPointvalue() == o2.getPointvalue()) {
                    return o1.getTypevalue() - o2.getTypevalue();
                } else {
                    return o1.getPointvalue() - o2.getPointvalue();
                }
            }
        });
    }
}
